package com.wisdom;


import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.wisdom.tools.HttpUtil;
import com.wisdom.view.HomeTabHostActivity;
import com.wisdom.view.LoginActivity;
import com.wisdom.view.SetPwdActivity;
import com.bluecall.sipua.R;

@SuppressLint("ShowToast")
public class MainActivity extends Activity {
	final String basicUrl="http://www.1000call.com";
    final String validateUrl=basicUrl+"/api/mobile/register";
    EditText edittext;
    Button btn_getcode;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		   requestWindowFeature(Window.FEATURE_NO_TITLE);
		    // hide statusbar of Android
		    // could also be done later
		    //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_main);
		initResourceRefs();
		}
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	public void goLogin(View view){
		startActivity(new Intent(MainActivity.this,LoginActivity.class));
	}
	@SuppressLint("ShowToast")
	public void getValidateCode(View view) throws JSONException{
		
		btn_getcode.setEnabled(false);
		String phone=edittext.getText().toString();
		JSONObject object=new JSONObject("{Phone:\""+phone+"\"}");
		JSONObject data=HttpUtil.post(validateUrl, object);
		if(data==null){
			Toast.makeText(getApplicationContext(), "获取 验证码失败：网络问题",Toast.LENGTH_SHORT);
			btn_getcode.setEnabled(true);
			btn_getcode.setText("获取失败，重新获取验证码！");
			return;
		}
		if(data.getBoolean("success")){
			btn_getcode.setText("验证码已发送到您手机,60秒后重新 获取");
			startAction();
		}else{
			Toast.makeText(getApplicationContext(), "获取 验证码失败："+data.getString("message"),Toast.LENGTH_SHORT).show();
			btn_getcode.setEnabled(true);
			btn_getcode.setText("获取失败，重新获取验证码！");
		}
	}
	private void startAction(){
		Intent intent=new Intent();
		 Bundle bundle=new Bundle();
	    bundle.putString("phone", edittext.getText().toString());
	    intent.putExtras(bundle);
		intent.setClass(MainActivity.this,SetPwdActivity.class);
		startActivity(intent);
	}
	
	void initResourceRefs(){
		edittext=(EditText)findViewById(R.id.edit_message);
		btn_getcode=(Button)findViewById(R.id.btn_getcode);
	}

		

}
